﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL.UserDSTableAdapters;
using WarehouseWebAPP.code.DAL;
using System.Security.Cryptography;

namespace WarehouseWebAPP.code.BLL
{
    public class UserBLL
    {
        private userTableAdapter UserAdapter = null;
        public userTableAdapter Adapter
        {
            get
            {
                if (UserAdapter==null) UserAdapter = new userTableAdapter();
                return UserAdapter;
            }
        }

        ///Dont delete!!!! , this is example to set the mapping for hiding the gid column in the gui
        /*public UserDS.userDataTable getUserDetail(string userID)
        {
            try { int.Parse(userID); }
            catch(Exception e) { throw new Exception("ID is not a Number");}
            UserDS.userDataTable table = adapter.UserGetByID(userID);
            if (table.Count == 0) throw new Exception("No users were found");
            //here i hide the num fucken column
            table.Columns[0].ColumnMapping = System.Data.MappingType.Hidden;
            return table;
        }
         */


        public void UserInsert(string user_id, string user_name, string user_address, string user_phone
            , string user_email, Guid user_type, string user_login_name, string user_password)
        {
            if (user_id == null)
                throw new Exception("User ID is missing");
            if (user_name == null)
                throw new Exception("user name is missing");
            if (user_type == null)
                throw new Exception("User type is missing");
            if (user_address == null)
                throw new Exception("User Address is missing");
            if (user_phone == null)
                throw new Exception("User Phone is missing");
            if (user_email == null)
                throw new Exception("User E-mail is missing");
            if (user_type == null)
                throw new Exception("User Type is missing");
            if (user_login_name == null)
                throw new Exception("User Login Name is missing");
            if (user_password == null)
                throw new Exception("User Password is missing");
            string hashedpassword = UserBLL.HashPassword(user_password);
            if (Adapter.UserInsert(user_id, user_name, user_address, user_phone, user_email,
                user_type, user_login_name, hashedpassword) < 1)
                throw new Exception("User was not added");
        }

        public void UserDelete(Guid user_num)
        {
            if (Adapter.UserDelete(user_num) < 1)
                throw new Exception("No user were deleted");
        }

        public UserDS.userDataTable UserGetByName(string user_name)
        {
            if(user_name == null)
                throw new Exception("User name is missing");
            return Adapter.UserGetByName(user_name);

        }
        public UserDS.userDataTable userGet()
        {
            return Adapter.GetData();
        }

        public UserDS.userDataTable userGetByID(string user_id)
        {
            if(user_id == null)
                throw new Exception("User ID is missing");
       
            return Adapter.UserGetByID(user_id);
        }

        public UserDS.userDataTable userGetByNum(Guid user_num)
        {
            if (user_num == null)
                throw new Exception("User Num is missing");

            return Adapter.UserGetByNum (user_num);
        }

        public UserDS.userDataTable userGetByType(Guid user_type)
        {
             if (user_type == null)
               throw new Exception("User type is missing");
             return Adapter.UserGetByType(user_type);
        }

        public UserDS.userDataTable userGetByLogin(string user_login_name)
        {
            if (user_login_name == null)
                throw new Exception("UserName is missing");
            return Adapter.UserGetByLogin(user_login_name);
        }

        public void UserUpdate(string user_id, string user_name, string user_address, string user_phone
            , string user_email, Guid user_type, string user_login_name, string user_password, Guid user_num)
        {
            if (user_num == null)
                throw new Exception("user Number is missing");
            if (user_id == null)
                throw new Exception("User ID is missing");
            if (user_name == null)
                throw new Exception("user name is missing");
            if (user_type == null)
                throw new Exception("User type is missing");
            if (user_address == null)
                throw new Exception("User Address is missing");
            if (user_phone == null)
                throw new Exception("User Phone is missing");
            if (user_email == null)
                throw new Exception("User E-mail is missing");
            if (user_type == null)
                throw new Exception("User Type is missing");
            if (user_login_name == null)
                throw new Exception("User Login Name is missing");
            if (user_password == null)
                throw new Exception("User Password is missing");
            string hashedpassword = UserBLL.HashPassword(user_password);
            if (Adapter.UserUpdate(user_id,user_name,user_address,user_phone,user_email,
                   user_type, user_login_name, hashedpassword, user_num) < 1)
                throw new Exception("User was not updated");
        }


        public static string HashPassword(string UnHashedPassword)
        {
            return Convert.ToBase64String(new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(UnHashedPassword)));
        }

        public void UserUpdatePassword(Guid user_num, string user_password)
        {
            if (user_num == null)
                throw new Exception("user Number is missing");
            if (user_password == null)
                throw new Exception("User Password is missing");
            string hashedpassword = UserBLL.HashPassword(user_password);
            if (Adapter.UserUpdatePassword(hashedpassword, user_num) < 1)
                throw new Exception("User Password was not updated");
        }


    }
}
